package com.java2007.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.java2007.dao.FoodDao;
import com.java2007.dao.FoodTypeDao;
import com.java2007.dao.impl.FoodDaoImpl;
import com.java2007.entity.PageBean;
import com.java2007.factory.BeanFactory;
import com.java2007.pojo.Food;
import com.java2007.pojo.FoodType;
import com.java2007.service.FoodService;
import com.java2007.util.JdbcUtil;

import java.sql.SQLException;
import java.util.List;

/**
 * 菜品业务实现
 *
 * @author ZOU
 */
public class FoodServiceImpl implements FoodService {
    private FoodDao fDao = (FoodDao) BeanFactory.getBean("com.java2007.dao.impl.FoodDaoImpl");
    private FoodTypeDao ftDao = (FoodTypeDao) BeanFactory.getBean("com.java2007.dao.impl.FoodTypeDaoImpl");


    /**
     * 查询所有
     *
     * @return
     * @throws SQLException
     */
    @Override
    public List<Food> findAll() throws SQLException {

        return fDao.selectAll();
    }

    /**
     * 根据名字查询
     *
     * @param foodName
     * @return
     * @throws SQLException
     */
    @Override
    public List<Food> findByFoodName(String foodName) throws SQLException {
        //判断输入框内是否输入值。输入：按照输入的名字调通过名字查询的方法
        //                      未输入：调用查询所有菜类别的方法
        if (StringUtils.isEmpty(foodName)) {
            foodName = "";
        } else {
            //去空格，防止输入空格导致无法查询
            foodName = foodName.trim();
        }
        //判断完得到的值肯定是 “”或“菜类别”
        return fDao.findByFoodName("%" + foodName + "%");
    }

    /**
     *
     * 根据ID查询
     * @param foodId
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteByFoodId(int foodId) throws SQLException {

        try {
            //开启事务
            JdbcUtil.begin();
            fDao.deleteByFoodId(foodId);
            //提交事务
            JdbcUtil.commit();

        } catch (Exception e) {
            e.printStackTrace();
            //回滚事务
            JdbcUtil.rollback();
            return 0;
        }

        return 1;
    }

    /**
     * 增加
     * @param food
     * @return
     * @throws SQLException
     */
    @Override
    public int saveFood(Food food) throws SQLException {
        try {
            //开启事务
            JdbcUtil.begin();

            FoodType foodType = ftDao.findByFoodTypeId(food.getTypeId());
            food.setTypeName(foodType.getTypeName());

            fDao.saveFood(food);
            //提交事务
            JdbcUtil.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            //回滚事务
            JdbcUtil.rollback();
            return 0;
        }
        return 1;
    }

    /**
     * 通过ID查询
     * @param foodId
     * @return
     * @throws SQLException
     */
    @Override
    public Food findByFoodId(int foodId) throws SQLException {
        return fDao.findByFoodId(foodId);
    }

    /**
     * 分页查询
     * @param pageNo
     * @param size
     * @param food
     * @return
     * @throws SQLException
     */
    @Override
    public PageBean<Food> findByPage(Integer pageNo, Integer size, Food food) throws SQLException {
        //判断其是否合理
        if (pageNo<0){
            pageNo=1;
        }
        //当前页
        List<Food> list=fDao.selectByPage((pageNo-1) * size,size,food);
        //总页数
        Long count=fDao.selectFoodCount(food);

        //将分页数据进行包装
        PageBean<Food> pageBean=new PageBean<>();
        pageBean.setPageNo(pageNo);
        pageBean.setList(list);
        pageBean.setTotalCount(count);
        pageBean.setTotalPages();
        return pageBean;
    }



    /**
     * 更新
     * @param food
     * @return
     * @throws SQLException
     */
    @Override
    public int updata(Food food) throws SQLException {
        try {
            //开启事务
            JdbcUtil.begin();
            fDao.updataFood(food);
            //提交事务
            JdbcUtil.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            //回滚事务
            JdbcUtil.rollback();
            return 0;
        }
        return 1;
    }




}
